(function () {
    'use strict';

    angular.module('KingAdmin.pages.pdev.device')
        .factory('DeviceService', DeviceService);

    /** @ngInject */
    function DeviceService($resource, $filter, toastr,CommonService) {

        var rest = $resource('pdev/device/:id', {}, {
            'create': {method: 'POST'},
            'update': {method: 'PUT'},
        });
        function getSmartData(param,callback) {
            $resource('pdev/device/getSmartData', {}, {
                'query': {method: 'POST'}
            }).query(param,
                function (data) {
                    console.log(data);
                    callback(data)
                }, function (error) {
                    toastr.error(error, "提示", {"progressBar": true,});
                });
        }

        function getByUid(param,callback) {
            $resource('pdev/device/getByUid', param, {
                'query': {method: 'GET'}
            }).query({},
                function (data) {
                    console.log(data);
                    callback(data)
                }, function (error) {
                    toastr.error(error, "提示", {"progressBar": true,});
                });
        }

        function del(param,callback) {
            CommonService.danger('确定删除?', function () {
                rest.delete(param,
                    function (data) {
                        if (data.code == 0) {
                            toastr.success("删除成功！", "提示", {"progressBar": true,});
                            updDevList();
                        } else {
                            toastr.warning("删除失败！", "提示", {"progressBar": true,});
                        }
                        callback(data);
                    }, function (error) {
                        toastr.error(error, "提示", {"progressBar": true,});
                    })
            })
        }
        function save(param,callback) {
            // CommonService.info('确定保存?',function () {
                if(angular.isDefined(param.id)&&param.id!=null){
                    rest.update(param,
                        function (data) {
                            console.log(data);
                            toastr.success("保存成功","提示",{"progressBar": true,});
                            updDevList();
                            callback(data);
                        }, function (error) {
                            toastr.error(error,"提示",{"progressBar": true,});
                        })
                }else{
                    rest.create(param,
                        function (data) {
                            console.log(data);
                            toastr.success("保存成功","提示",{"progressBar": true,});
                            updDevList();
                            callback(data);
                        }, function (error) {
                            toastr.error(error,"提示",{"progressBar": true,});
                        })
                }
            // });
        }
        function getInfo(param,callback) {
            rest.get(param,
                function (data) {
                    console.log(data);
                    callback(data);
                }, function (error) {
                    toastr.error(error,"提示",{"progressBar": true,});
                })
        }
        function getList(param,callback) {
            $resource('pdev/device/getlist').get(param,
                function (data) {
                    console.log(data);
                    callback(data);
                }, function (error) {
                    toastr.error(error,"提示",{"progressBar": true,});
                })
        }

        function getAuthList(param,callback) {
            $resource('pdev/device/getAuthList').get(param,
                function (data) {
                    console.log(data);
                    callback(data);
                }, function (error) {
                    toastr.error(error,"提示",{"progressBar": true,});
                })
        }

        function getDictList(callback) {
            $resource('pdev/device/getDictList').get({},
                function (data) {
                    console.log(data);
                    callback(data);
                }, function (error) {
                    toastr.error(error,"提示",{"progressBar": true,});
                })
        }

        function getGateway(devUid, callback) {
            $resource('pdev/device/getGateway').get({devUid: devUid},
                function (data) {
                    console.log(data);
                    callback(data);
                }, function (error) {
                    toastr.error(error,"提示",{"progressBar": true,});
                })
        }

        // 获取设备字典列表
        var devList = [];
        updDevList();

        function updDevList() {
            $resource('pdev/device/getDictList').get({},
                function (data) {
                    console.log(data);
                    devList = data.result;
                }, function (error) {
                    console.log(error);
                });
        }

        var optDevList = [];
        updOptDevList();
        function updOptDevList() {
            $resource('pdev/device/getOptDevList').get({},
                function (data) {
                    console.log(data);
                    optDevList = data.result;
                }, function (error) {
                    console.log(error);
                });
        }
        function getOptDevList() {
            return optDevList;
        }

        function getDevList() {
            return devList;
        }

        function showDevName(deviceUid) {
            var selected = [];
            if (devList.length > 0) {
                if (deviceUid || deviceUid == 0) {
                    selected = $filter('filter')(devList, {value: deviceUid});
                }
                return selected.length ? selected[0].label : '未知';
            }

            return '未知';
        }

        return {
            getSmartData:getSmartData,
            del:del,
            save:save,
            getInfo:getInfo,
            getByUid:getByUid,
            getList:getList,
            getDictList:getDictList,
            showDevName:showDevName,
            getDevList: getDevList,
            getOptDevList: getOptDevList,
            getAuthList: getAuthList,
            getGateway: getGateway,
        };

    }

})();
